<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.warehouseName" size="small" placeholder="仓库代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="handleFilter">查询</el-button>
      <el-button v-permiss="['60203070']" style="float:right" size="small" type="primary" round @click="showImport">导入</el-button>
      <el-button v-permiss="['60203020']" size="small" type="primary" style="float:right" round @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="仓库代码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.warehouseCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="仓库名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.warehouseName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="负责人" align="center">
          <template slot-scope="{row}">
            <span>{{ row.manager }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center">
          <template slot-scope="{row}">
            <span>{{ row.tel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否主仓库" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isMain==1?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否虚拟仓库" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isVirtual==1?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="允许负库存" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isNegative==1 ? '允许' : '不允许' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否冷藏" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isRefrigerated==1 ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center">
          <template slot-scope="{row}">
            <el-switch v-model="row.isDisable" :active-value="0" :inactive-value="1" @change="updateStatus($event, row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="{row}">
            <el-button v-permiss="['60203030']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['60203060']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增仓库':'修改仓库'" :visible.sync="dialogVisible" width="600px">
      <el-form ref="dataForm" :rules="rules" :model="form" :inline="true" label-position="right" label-width="100px" style="width: 560px; margin-left:5px;">
        <el-form-item label="仓库代码" prop="warehouseCode">
          <el-input v-model="form.warehouseCode" placeholder="仓库代码" />
        </el-form-item>
        <el-form-item label="是否主仓库" prop="isMain">
          <el-radio v-model="form.isMain" :label="1">是</el-radio>
          <el-radio v-model="form.isMain" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="仓库名称" />
        </el-form-item>
        <el-form-item label="是否虚拟仓库" prop="isVirtual">
          <el-radio v-model="form.isVirtual" :label="1">是</el-radio>
          <el-radio v-model="form.isVirtual" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="负责人" prop="manager">
          <el-input v-model="form.manager" placeholder="负责人" />
        </el-form-item>
        <el-form-item label="允许负库存" prop="isNegative">
          <el-radio v-model="form.isNegative" :label="1">是</el-radio>
          <el-radio v-model="form.isNegative" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="电话" />
        </el-form-item>
        <el-form-item label="是否冷藏" prop="isRefrigerated">
          <el-radio v-model="form.isRefrigerated" :label="1">是</el-radio>
          <el-radio v-model="form.isRefrigerated" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="warehouse" title="仓库" />
  </div>
</template>

<script>
import { getWarehouse, saveWarehouse, delWarehouse, updateWarehouseDisabled } from '@/api/basedata'
import importData from './importData'
export default {
  name: 'BaseWarehouse',
  components: { importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        warehouseName: ''
      },
      form: {
        warehouseName: '',
        warehouseCode: '',
        manager: '',
        tel: '',
        isMain: 1,
        isVirtual: 0,
        isNegative: 1,
        isRefrigerated: 0,
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        warehouseName: [{ required: true, message: '仓库名称不能为空', trigger: 'change' }],
        warehouseCode: [{ required: true, message: '仓库代码不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    getList() {
      this.listLoading = true
      getWarehouse(this.listQuery).then(response => {
        this.listLoading = false
        this.tableData = response.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      this.form.warehouseName = ''
      this.form.warehouseCode = ''
      this.form.isMain = 1
      this.form.isNegative = 1
      this.form.isVirtual = 0
      this.form.isRefrigerated = 0
      this.form.isDisable = 0
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      this.form.id = obj.id
      this.form.warehouseName = obj.warehouseName
      this.form.warehouseCode = obj.warehouseCode
      this.form.isMain = obj.isMain
      this.form.isNegative = obj.isNegative
      this.form.isVirtual = obj.isVirtual
      this.form.isRefrigerated = obj.isRefrigerated
      this.form.isDisable = obj.isDisable
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    updateStatus(e, row) {
      this.changeAvaiable(e, row.id)
    },
    changeAvaiable(isDisable, id) {
      var obj = { isDisable, id }
      updateWarehouseDisabled(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    handleDel(id) {
      this.$confirm('你确认要删除吗?', '提示', {
        confirmButtonText: '确定', closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveWarehouse(this.form).then(res => {
            if (res.data.errorCode == 0) {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveWarehouse(this.form).then(res => {
            if (res.data.errorCode == 0) {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delWarehouse(id).then(res => {
        if (res.data.errorCode == 0) {
          this.getList()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style scoped>
.curTit{font-size: 14px;margin-bottom: 10px}
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
